﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class LarGrid_Int : LarGridPropertyHandler
    {
        public LarTrackBar TrackBar;

        public Range Range;

        public LarPropertyGrid PropertyGrid;

        private string PropertyName;
        public LarGrid_Int(Camera2D Camera, LarPropertyGrid grid, PropertyInfo info, object property)
            : base(Camera, info, property)
        {
            object[] attributes = info.GetCustomAttributes(typeof(Range), true);
            if (attributes.Length == 0)
            {
                Range = new Range(-5, 5);
            }
            else
            {
                Range = (Range)attributes[0];
            }

            this.PropertyGrid = grid;

            this.BackgroundRectangle = grid.GetNextRectangle(40);

            TrackBar = new LarTrackBar((int)((float)grid.Width * 0.55f), 10,
                BackgroundRectangle.X + (int)(grid.Width * 0.3f), BackgroundRectangle.Y, 32, 32,
                Range.Min, Range.Max);
            TrackBar.OnSlide += new EventHandler(TrackBar_OnSlide);
            TrackBar.Camera = this.Camera;

            TrackBar.CurrentValue = (int)PropertyInfo.GetValue(property, null);
            PropertyName = PropertyInfo.Name;
        }

        void TrackBar_OnSlide(object sender, EventArgs e)
        {
            PropertyInfo.SetValue(PropertyGrid.SelectedObject, (int)TrackBar.CurrentValue, null);
            PropertyGrid.LastPropertyClicked = this;
        }

        public override void Update()
        {
            base.Update();

            TrackBar.Update();
        }

        public override void Draw()
        {
            base.Draw();

            string TrackCurrent = PropertyInfo.GetValue(PropertyGrid.SelectedObject, null).ToString();
            string Fontname = "LucidaConsole12";
            Engine.RenderManager.DrawString(TrackCurrent, 0, 255,
                new Vector2(TrackBar.CurrentSliderPosition + TrackBar.MinX,
                TrackBar.SliderRectangle.Y - 20), Fontname, Color.White, Camera);

            Engine.RenderManager.DrawString(PropertyName, 0, 255, new Vector2(
                BackgroundRectangle.X + 10, BackgroundRectangle.Y), Fontname, Color.White, Camera);

            Engine.RenderManager.DrawString(TrackBar.Minimum.ToString(), 0, 255, new Vector2(
                TrackBar.TrackRectangle.X - (TrackBar.Minimum.ToString().Length * 10) - 3, TrackBar.TrackRectangle.Y), Fontname,
                Color.Red, Camera);
            Engine.RenderManager.DrawString(TrackBar.Maximum.ToString(), 0, 255, new Vector2(
                TrackBar.TrackRectangle.X + TrackBar.TrackRectangle.Width + 5, TrackBar.TrackRectangle.Y), Fontname,
                Color.Red, Camera);

            TrackBar.Draw();
        }
    }
}
